package com.dave.gd.core.common.globalsession.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dave.gd.core.common.globalsession.GlobalSessionHttpRequest;
import com.dave.gd.core.common.globalsession.StoreKeyGenerator;
import com.dave.gd.core.common.globalsession.manager.SessionManager;

@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet {
    private static final String CONTENT_TYPE     = "text/html; charset=UTF-8";
    private static final String METADATA_KEY     = "__metadata__";
    private static final String GLOBAL_NAMESPACE = "GLOBAL";
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        
        // 设置页面字符集
        response.setContentType(CONTENT_TYPE);
        SessionManager sm = new SessionManager();
        StoreKeyGenerator keyGenerator = null;
        String username = request.getParameter("username");
        String password = request.getParameter("pass");
        
        if (username == null || password == null || "".equals(username.trim())
            || "".equals(password)) {
            response.sendRedirect("login.jsp");
            return;
        }
        if (("kandy".equals(username) || "ideal".equals(username)) && "123".equals(password)) {
            // 将用户登录信息存入session
            String sessionIdValue = request.getSession().getId();
            keyGenerator = new StoreKeyGenerator(sessionIdValue, GLOBAL_NAMESPACE);
            // SessionStore store = new RedisSessionStore(
            // SessionManager.getRedisServerAddresses());
            
            GlobalSessionHttpRequest wrappedReq = new GlobalSessionHttpRequest(request,
                sessionIdValue, GLOBAL_NAMESPACE, 600);
            wrappedReq.getSession().setAttribute("username", username);
            wrappedReq.getSession().setAttribute("password", password);
            
            // 在redis数据库中添加session索引
            sm.addSessionIndex(keyGenerator.generate(METADATA_KEY), username);
            
            // 添加Cookie便于在filter进行是否登录的验证
            Cookie sessionIdCookie = new Cookie("__GSID__", sessionIdValue);
            response.addCookie(sessionIdCookie);
            
            // response.sendRedirect("index.jsp");
            request.getRequestDispatcher("index.jsp").forward(wrappedReq, response);
            return;
        }
        if ("admin".equals(username) && "123".equals(password)) {
            // 将用户登录信息存入session
            String sessionIdValue = request.getSession().getId();
            keyGenerator = new StoreKeyGenerator(sessionIdValue, GLOBAL_NAMESPACE);
            GlobalSessionHttpRequest wrappedReq = new GlobalSessionHttpRequest(request,
                sessionIdValue, GLOBAL_NAMESPACE, 600);
            wrappedReq.getSession().setAttribute("username", username);
            wrappedReq.getSession().setAttribute("password", password);
            
            // 在redis数据库中添加session索引
            sm.addSessionIndex(keyGenerator.generate(METADATA_KEY), username);
            
            // 添加Cookie便于在filter进行是否登录的验证
            Cookie sessionIdCookie = new Cookie("__GSID__", sessionIdValue);
            response.addCookie(sessionIdCookie);
            response.sendRedirect(request.getContextPath() + "/listSession.do");
            return;
        }
        response.sendRedirect("login.jsp");
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        doGet(req, resp);
    }
    
    @Override
    public void init() throws ServletException {
        super.init();
    }
    
}
